from django.db import models
from home.models import MyUser
from django.utils import timezone
from django.db import models
from mdeditor.fields import MDTextField


# Create your models here.

LESSON_STATUS = {
    ("no", "未上课"),
    ("normal", "正常上课"),
    ("ask_for_leave", "请假"),
}


class Lesson_type(models.Model):
    """
    课程类型
    """

    lesson_type = models.CharField(max_length=1024, verbose_name="课程类型")
    price = models.FloatField(default=300, verbose_name="单价")
    duration = models.FloatField(default=90, verbose_name="课程时长")
    teacher = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="维护教师",
        blank=True,
        null=True,
    )

    class Meta:
        verbose_name = "课程类型"
        verbose_name_plural = "00-课程类型"  # 侧边栏

    def __str__(self):
        return str(self.lesson_type)


class Payment(models.Model):
    """
    交费记录
    """

    user = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="学员",
        related_name="student",
    )
    workload = models.FloatField(verbose_name="新增课时", default=0, blank=True)
    lesson_type = models.ForeignKey(
        Lesson_type,
        on_delete=models.CASCADE,
        verbose_name="课程类型",
        default=None,
    )
    time = models.DateTimeField(verbose_name="交费时间", default=timezone.now)
    note = MDTextField(verbose_name="备注", default=" ", null=True, blank=True)
    teacher = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="授课教师",
        blank=True,
        null=True,
        related_name="pay_teacher",
    )
    note_user = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="收款人",
        related_name="pay_note_user",
        default=1,
        blank=True,
    )
    workload_note = models.FloatField(verbose_name="剩余课时", default=0)
    money = models.FloatField(verbose_name="新增费用", default=0, blank=True)

    class Meta:
        verbose_name = "交费记录"
        verbose_name_plural = "01-交费记录"  # 侧边栏
        ordering = ("time",)


class Scheduling(models.Model):
    """排课"""

    user = models.ManyToManyField(
        MyUser,
        verbose_name="学员",
    )
    lesson_type = models.ForeignKey(
        Lesson_type,
        on_delete=models.CASCADE,
        verbose_name="课程类型",
        default=None,
    )
    title = models.CharField(max_length=1024, verbose_name="课程名称")
    start = models.DateTimeField(verbose_name="上课时间", default=timezone.now)
    notice = MDTextField(verbose_name="课程预告", default=None, blank=True)
    teacher = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="授课教师",
        blank=True,
        null=True,
        related_name="sch_teacher",
    )
    status = models.CharField(
        verbose_name="上课状态",
        default="normal",
        choices=LESSON_STATUS,
        max_length=20,
    )

    note_user = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="排课人",
        related_name="sch_note_user",
        blank=True,
    )

    class Meta:
        verbose_name = "排课记录"
        verbose_name_plural = "02-排课记录"  # 侧边栏
        ordering = ("start",)

    def __str__(self):
        return f"{self.id}.{self.title}"

    def short_notice(self):
        # 控制显示长度，必须在adminx的list_display变量中改为函数名
        if len(str(self.notice)) > 30:
            return "{}...".format(str(self.notice)[0:29])
        else:
            return str(self.notice)


class Lesson_note(models.Model):
    """
    上课记录
    """

    user = models.ManyToManyField(
        MyUser,
        verbose_name="学员",
    )
    title = models.CharField(max_length=1024, verbose_name="课程名称")
    lesson_type = models.ForeignKey(
        Lesson_type,
        on_delete=models.CASCADE,
        verbose_name="课程类型",
        default=None,
    )
    start = models.DateTimeField(verbose_name="上课时间", default=timezone.now)
    end = models.DateTimeField(verbose_name="下课时间", default=timezone.now)
    notice = models.ForeignKey(
        Scheduling,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        verbose_name="排课",
    )  # 关联排课，如果选了就可以设置成已经上课
    note = MDTextField(verbose_name="课堂记录", blank=True, default="""今天我们学习的是《 》，本次课的教学目的有：

1. 
2. 
3. 

重点知识和作业如下：
## 一、重点知识
### 1. 
### 2. 
### 3.

## 二、上课情况
### **1. 表现情况**
- 今天同学们的表现得非常不错，都能很顺利完成教学任务，其中：
	- **xxx**同学能够**xxxx**
	- **xxx**同学上课时候**xxxx**
	- **xxx**同学对老师讲的知识点**xxxx**
	
- 希望同学们下次课继续努力，保持这种上课态度。

### **2. 课堂评价**                   

| 姓名    | 作业       | 注意力     | 代码力     | 创造力      | 思维力       |
| ------- | ---------- | ---------- | ---------- | ----------- | ---------- |
| **XXX** | ★★★★☆ | ★★★★★ | ★★★★★ | ★★★★★  | ★★★★★ |
| **XXX** | ★★★★★ | ★★★★★ | ★★★★★ | ★★★★★  | ★★★★★ |
| **XXX** | ★★★★★ | ★★★★★ | ★★★★★ | ★★★★★  | ★★★★★ |
                       
## 三、课后作业
今天的作业有下面几部分：
                       
1. 
2. 
3. 
""")

    status = models.CharField(
        verbose_name="状态",
        choices=LESSON_STATUS,
        blank=True,
        default="normal",
        max_length=20,
    )
    teacher = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="授课教师",
        blank=True,
        null=True,
        related_name="lesson_teacher",
    )
    note_user = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="记录人",
        related_name="lesson_note_user",
        default=1,
        blank=True,
    )
    consume = models.FloatField(verbose_name="消耗课时", default=2)

    class Meta:
        verbose_name = "上课记录"
        verbose_name_plural = "03-上课记录"  # 侧边栏
        ordering = ("start",)

    def __str__(self):
        return f"{self.id}.{self.title}"

    def short_note(self):
        # 控制显示长度，必须在adminx的list_display变量中改为函数名
        if len(str(self.note)) > 30:
            return "{}...".format(str(self.note)[0:29])
        else:
            return str(self.note)


class Consume_note(models.Model):
    """消课记录"""

    user = models.ForeignKey(
        MyUser, on_delete=models.CASCADE, verbose_name="学员")
    lesson_type = models.ForeignKey(
        Lesson_type,
        on_delete=models.CASCADE,
        verbose_name="课程类型",
        default=None,
    )
    status = models.CharField(
        verbose_name="状态",
        choices=LESSON_STATUS,
        blank=True,
        default="normal",
        max_length=20,
    )
    lesson_note = models.ManyToManyField(
        Lesson_note(),
        verbose_name="上课记录",
    )  # 可以关联上课记录
    teacher = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="授课教师",
        blank=True,
        null=True,
        related_name="con_teacher",
    )
    consume = models.FloatField(verbose_name="消耗课时", default=1)
    note = MDTextField(verbose_name="备注", blank=True, default=None)
    note_user = models.ForeignKey(
        MyUser,
        on_delete=models.CASCADE,
        verbose_name="记录人",
        related_name="con_note_user",
        default=1,
        blank=True,
    )
    workload_note = models.FloatField(verbose_name="剩余课时", default=0)

    class Meta:
        verbose_name = "消课记录"
        verbose_name_plural = "04-消课记录"  # 侧边栏


# Lesson_note.short_note.allow_tags = True
Lesson_note.short_note.short_description = "上课记录"  # 自定义名称
Scheduling.short_notice.short_description = "课程预告"  # 自定义名称
